package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.damain.dto.PageDTO;
import com.example.mybatisplus.damain.po.Student;
import com.example.mybatisplus.damain.query.PageQuery;
import com.example.mybatisplus.damain.vo.StudentVo;
import com.example.mybatisplus.mapper.StudentMapper;
import com.example.mybatisplus.service.IStudentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.utils.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

import static cn.hutool.core.bean.BeanUtil.toBean;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author bilal
 * @since 2023-08-29
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Override
    public PageDTO<StudentVo> queryStudent(PageQuery query) {
        Page<Student> page = lambdaQuery()
                .page(query.toMpPage(query.getSortBy(),query.getIsAsc()));
        List<Student> records = page.getRecords();
        ArrayList<StudentVo> studentVoList = new ArrayList<>(records.size());
        for (Student record : records) {
            StudentVo studentVo = BeanUtils.copyBean(record, StudentVo.class);
            studentVoList.add(studentVo);
        }
        return PageDTO.of(page,studentVoList);
    }

    @Override
    public Page<Student> queryStudent2(PageQuery query) {
        Page<Student> page = lambdaQuery()
                .page(query.toMpPage(query.getSortBy(),query.getIsAsc()));
        return page;
    }
}
